package tps.video_games;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.uqbar.vainilla.DesktopGameLauncher;
import com.uqbar.vainilla.Game;
import com.uqbar.vainilla.GameScene;

public class Arkanoid extends Game {
	
	List<Ladrillo> ladrillos; 
	Paleta paleta;
	List<Bolita> bolitas;
	List<List<Ladrillo>> niveles;
	List<Vida> vidas;
	Puntaje puntaje;
	
	//INICIALIZACION DE NIVELES
	public List<Ladrillo> ladrillosNivel1(){
		List<Ladrillo>  ladrillos = new ArrayList<Ladrillo>();
		ladrillos.add(new Ladrillo(100, 15, Color.CYAN));
		ladrillos.add(new Ladrillo(200, 15, Color.CYAN));
		ladrillos.add(new Ladrillo(300, 15, Color.BLACK,2));
		ladrillos.add(new Ladrillo(400, 15, Color.BLACK,2));
		ladrillos.add(new Ladrillo(500, 15, Color.CYAN));
		ladrillos.add(new Ladrillo(600, 15, Color.CYAN));
		ladrillos.add(new Ladrillo(700, 15, Color.CYAN,new PildoraBolaLenta()));
		
		ladrillos.add(new Ladrillo(100, 60, Color.WHITE,new PildoraMultibola()));
		ladrillos.add(new Ladrillo(200, 60, Color.WHITE));
		ladrillos.add(new Ladrillo(300, 60, Color.BLACK,2));
		ladrillos.add(new Ladrillo(400, 60, Color.BLACK,2));
		ladrillos.add(new Ladrillo(500, 60, Color.WHITE));
		ladrillos.add(new Ladrillo(600, 60, Color.WHITE));
		ladrillos.add(new Ladrillo(700, 60, Color.WHITE,new PildoraModoPegajoso()));
		
		ladrillos.add(new Ladrillo(100, 105, Color.CYAN));
		ladrillos.add(new Ladrillo(200, 105, Color.CYAN));
		ladrillos.add(new Ladrillo(300, 105, Color.BLACK,2,new PildoraLaser()));
		ladrillos.add(new Ladrillo(400, 105, Color.BLACK,2));
		ladrillos.add(new Ladrillo(500, 105, Color.CYAN));
		ladrillos.add(new Ladrillo(600, 105, Color.CYAN,new PildoraAchicaPaleta()));
		ladrillos.add(new Ladrillo(700, 105, Color.CYAN));
		
		ladrillos.add(new Ladrillo(100, 150, Color.CYAN,new PildoraEnsanchaPaleta()));
		ladrillos.add(new Ladrillo(200, 150, Color.CYAN));
		ladrillos.add(new Ladrillo(300, 150, Color.BLACK,2));
		ladrillos.add(new Ladrillo(400, 150, Color.BLACK,2));
		ladrillos.add(new Ladrillo(500, 150, Color.CYAN));
		ladrillos.add(new Ladrillo(600, 150, Color.CYAN));
		ladrillos.add(new Ladrillo(700, 150, Color.CYAN));
		return ladrillos;
	}
	
	public List<Ladrillo> ladrillosNivel2(){
		List<Ladrillo>  ladrillos = new ArrayList<Ladrillo>();
		ladrillos.add(new Ladrillo(10, 15, Color.YELLOW));
		ladrillos.add(new Ladrillo(100, 15, Color.YELLOW));
		ladrillos.add(new Ladrillo(200, 15, Color.YELLOW));
		ladrillos.add(new Ladrillo(300, 15, Color.YELLOW));
		ladrillos.add(new Ladrillo(300, 15, Color.YELLOW));
		ladrillos.add(new Ladrillo(400, 15, Color.YELLOW));
		ladrillos.add(new Ladrillo(500, 15, Color.YELLOW));
		ladrillos.add(new Ladrillo(600, 15, Color.YELLOW));
		ladrillos.add(new Ladrillo(700, 15, Color.YELLOW));
		ladrillos.add(new Ladrillo(800, 15, Color.YELLOW));
		
		ladrillos.add(new Ladrillo(10, 60, Color.BLUE));
		ladrillos.add(new Ladrillo(100, 60, Color.BLUE));
		ladrillos.add(new Ladrillo(200, 60, Color.BLUE));
		ladrillos.add(new Ladrillo(300, 60, Color.BLUE));
		ladrillos.add(new Ladrillo(400, 60, Color.BLUE,new PildoraLaser()));
		ladrillos.add(new Ladrillo(500, 60, Color.BLUE));
		ladrillos.add(new Ladrillo(600, 60, Color.BLUE));
		ladrillos.add(new Ladrillo(700, 60, Color.BLUE,new PildoraAchicaPaleta()));
		ladrillos.add(new Ladrillo(800, 60, Color.BLUE));
		
		ladrillos.add(new Ladrillo(10, 105, Color.BLACK,3,new PildoraModoPegajoso()));
		ladrillos.add(new Ladrillo(100, 105, Color.BLACK,3));
		ladrillos.add(new Ladrillo(200, 105, Color.BLACK,3));
		ladrillos.add(new Ladrillo(300, 105, Color.BLACK,3));
		ladrillos.add(new Ladrillo(400, 105, Color.BLACK,3,new PildoraEnsanchaPaleta()));
		ladrillos.add(new Ladrillo(500, 105, Color.BLACK,3));
		ladrillos.add(new Ladrillo(600, 105, Color.BLACK,3));
		ladrillos.add(new Ladrillo(700, 105, Color.BLACK,3));
		ladrillos.add(new Ladrillo(800, 105, Color.BLACK,3,new PildoraMultibola()));
		
		ladrillos.add(new Ladrillo(10, 150, Color.RED));
		ladrillos.add(new Ladrillo(100, 150, Color.RED));
		ladrillos.add(new Ladrillo(200, 150, Color.RED));
		ladrillos.add(new Ladrillo(300, 150, Color.RED));
		ladrillos.add(new Ladrillo(400, 150, Color.RED));
		ladrillos.add(new Ladrillo(500, 150, Color.RED,new PildoraBolaLenta()));
		ladrillos.add(new Ladrillo(600, 150, Color.RED));
		ladrillos.add(new Ladrillo(700, 150, Color.RED));
		ladrillos.add(new Ladrillo(800, 150, Color.RED));
		return ladrillos;
	}
	
	//COMPORTAMIENTO
	@Override
	public Dimension getDisplaySize() {
		return new Dimension(900, 700);
	}

	@Override
	public String getTitle() {
		return "Arkanoid";
	}

	@Override
	protected void initializeResources() {
		this.puntaje = new Puntaje(875, 10);
		this.paleta = new Paleta();
		this.bolitas = new ArrayList<Bolita>();
		this.bolitas.add(new Bolita(this.paleta));
		this.niveles = new ArrayList<List<Ladrillo>>();
		this.niveles.add(ladrillosNivel2());
		this.ladrillos = this.ladrillosNivel1();
		this.vidas = new ArrayList<Vida>();
		this.vidas.add(new Vida(880,660));
		this.vidas.add(new Vida(865,660));
	}

	@Override
	protected void setUpScenes() {
		this.getCurrentScene().addComponents(this.bolitas);	
		this.getCurrentScene().addComponent(this.paleta);		
		this.getCurrentScene().addComponents(this.ladrillos);			
		this.getCurrentScene().addComponent(this.puntaje);
		this.getCurrentScene().addComponents(this.vidas);
	}
	
	@Override
	public void takeStep(Graphics2D graphics) {
		super.takeStep(graphics);
		
		if(this.ladrillos.isEmpty()){
			if(!this.niveles.isEmpty()){
				JOptionPane.showMessageDialog(null, "PASASTE AL SIGUIENTE NIVEL =)");
				this.pasarAlSiguienteNivel();
			}else{
				JOptionPane.showMessageDialog(null, "FELICITACIONES GANASTE EL JUEGO =D");
				System.exit(0);
			}

		}
	}
	
	public void pasarAlSiguienteNivel(){
		this.setCurrentScene(new GameScene());
		this.sacarPoderes();		
		this.ladrillos = this.niveles.remove(0);
		this.bolitas.removeAll(this.bolitas);
		this.bolitas.add(new Bolita(this.paleta));
		this.setUpScenes();
	}
	
	public void agregarVida(){
		if(this.vidas.isEmpty()){
			Vida v = new Vida(880,660);
			this.vidas.add(v);
			this.getCurrentScene().addComponent(v);
		}else{
			Vida ultimaVida = this.vidas.get(this.vidas.size()-1);
			Vida v = new Vida(ultimaVida.getX() - 15,660);
			this.vidas.add(v);
			this.getCurrentScene().addComponent(v);
		}
	}
	
	public void quitarVida(){
		this.sacarPoderes();
		if(this.vidas.isEmpty()){
			JOptionPane.showMessageDialog(null, "Perdiste. No te quedan vidas =(");
			System.exit(0);
		}else{			
			Vida vida = this.vidas.remove(this.vidas.size()-1);
			vida.destroy();
			JOptionPane.showMessageDialog(null, "Perdiste una vida =(");
			Bolita b = new Bolita(this.paleta);
			this.bolitas.add(b);
			this.getCurrentScene().addComponent(b);
		}

	}
	
	public void multibola() {
		this.sacarPoderes();
		Bolita b1 = new Bolita(this.paleta,this.bolitas.get(0));
		Bolita b2 = new Bolita(this.paleta,this.bolitas.get(0));
		this.bolitas.add(b1);
		this.bolitas.add(b2);	
		this.getCurrentScene().addComponent(b1);
		this.getCurrentScene().addComponent(b2);
	}	
	
	public void sacarPoderes(){
		for(Bolita b: this.bolitas){
			b.setVelocidadNormal();
			b.setModoPegajoso(false);
		}
		this.paleta.reinicializar();
	}

	public void quitarBolita(Bolita b) {
		b.destroy();
		this.bolitas.remove(b);		
		if(this.bolitas.isEmpty())
			this.quitarVida();
	}
	
	public void sumarPuntos(int p){
		this.puntaje.sumarPuntos(p);
	}

	public static void main(String[] args) {
		new DesktopGameLauncher(new Arkanoid()).launch();
	}
	
	//ACCESORS
	public Paleta getPaleta() {
		return paleta;
	}
	
	public List<Ladrillo> getLadrillos() {
		return ladrillos;
	}
	
	public Bolita getBolita() {
		return this.bolitas.get(0);
	}
	
	public List<Bolita> getBolitas() {
		return bolitas;
	}

}